package forms;



import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import baseclasses.Course;

import com.devcon5.StudentPlanner.R;

import database.CourseDAO;

public class GradeForm extends Activity {
	//views, widgets, and formatting options
	protected Spinner courseSpinner;
	protected EditText courseTitle, scoreEditText, outOfEditText;
	protected TextView percentage;
	protected double score, outOf;
	protected TextWatcher textChangedListener;
	protected DecimalFormat df = new DecimalFormat("#.##");
	protected ArrayAdapter<Course> adapter;
	protected ArrayList<Course> courses;
	protected Button addButton, cancelButton;

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addgrade);

		courseTitle = (EditText)findViewById(R.id.addgrade_gradetitle);
		scoreEditText = (EditText)findViewById(R.id.addgrade_scoreedittext);
		percentage = (TextView)findViewById(R.id.addgrade_percenttextview);
		outOfEditText = (EditText)findViewById(R.id.addgrade_outofedittext);
		courseSpinner = (Spinner)findViewById(R.id.addgrade_coursespinner);
		addButton = (Button)findViewById(R.id.addgrade_addbutton);
		cancelButton = (Button)findViewById(R.id.addgrade_cancelbutton);

		//get array list of courses from database for spinner
		courses = CourseDAO.getCourses( GradeForm.this );

		adapter = new ArrayAdapter<Course>(this,android.R.layout.simple_spinner_item, courses);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		courseSpinner.setAdapter(adapter);

		//	listener for showing the percentage of grade dynamically
		textChangedListener = new TextWatcher() {

			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
				try
				{
					//if there is text in the score textbox, parse it to double...
					if(scoreEditText.getText().toString().length()>0){
						score = Double.parseDouble(scoreEditText.getText().toString());
					}
					else{
						score = 0;
					}
					if(outOfEditText.getText().toString().length()>0){ //...text in the outof, parse it to double & set %
						outOf = Double.parseDouble(outOfEditText.getText().toString());
						percentage.setText(" = " +df.format(score/outOf*100) +"%");
					}
					else{
						outOf = 0;	//no text in the out of, assume it's extra points added
						percentage.setText(" = extra credit");
					}
				}
				catch( NumberFormatException e)
				{
					e.printStackTrace();
				}

			}

			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		};

		scoreEditText.addTextChangedListener(textChangedListener); //add the textchangedlistener
		outOfEditText.addTextChangedListener(textChangedListener);	//add the textchangedlistener

		addButton.setOnClickListener(new OnClickListener(){
			
			public void onClick(View arg0) {
				//method is different in add course and edit course
				addButtonClicked();

			}
		});


		cancelButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				onBackPressed();
				finish();
			}
		});


	}

	//	get course position from the course array list
	public int getCoursePosition(int courseId){
		for(int i=0;i<courses.size();i++){
			if(courses.get(i).getId()==courseId){
				return i;
			}
		}
		return 0;
	}

	//	called when add button is clicked
	public void addButtonClicked()
	{
		//overriden by child classes
	}


}
